package com.newasia.server.common.result;

import com.newasia.server.common.exception.BaseBusinessException;

/**
 * 腾讯课堂搜索【咕泡学院】
 * 官网：www.gupaoedu.com
 * 风骚的Mic 老师
 * create-date: 2019/7/22-18:45
 */
public class ResponseUtil<T> {

    private ResponseData<T> responseData;

    public ResponseUtil() {
        responseData = new ResponseData<>();
        responseData.setSuccess(true);
        responseData.setMessage("success");
        responseData.setCode("200");
    }

    public ResponseData<T> setData(ResponseData data) {
        this.responseData = data;
        if(!data.getCode().equals(ManagementRetCode.SUCCESS.getCode()))
            data.setSuccess(false);
        return this.responseData;
    }

    /*public ResponseData<T> setData(T t) {
        this.responseData.setResult(t);
        this.responseData.setSuccess(true);
        responseData.setCode("200");
        return this.responseData;
    }

    public ResponseData<T> setData(T t, String msg) {
        this.responseData.setResult(t);
        this.responseData.setSuccess(true);
        this.responseData.setMessage(msg);
        responseData.setCode("200");
        return this.responseData;
    }*/

    public ResponseData<T> setErrorMsg(BaseBusinessException exception) {
        this.responseData.setSuccess(false);
        this.responseData.setMessage(exception.getMessage());
        this.responseData.setCode(exception.getErrorCode());
        return this.responseData;
    }

    public ResponseData<T> setErrorMsg() {
        this.responseData.setSuccess(false);
        this.responseData.setMessage("服务器内部错误");
        this.responseData.setCode("500");
        return this.responseData;
    }

    public ResponseData<T> setErrorMsg(ResponseData data) {
        this.responseData = data;
        data.setSuccess(false);
        return this.responseData;
    }

    public ResponseData<T> setSuccessMsg(ResponseData data) {
        this.responseData = data;
        data.setSuccess(true);
        return this.responseData;
    }

    /*public ResponseData<T> setErrorMsg(String code, String msg) {
        this.responseData.setSuccess(false);
        this.responseData.setMessage(msg);
        responseData.setCode(code);
        return this.responseData;
    }*/

    /*public ResponseData<T> setErrorMsg(T t) {
        this.responseData.setResult(t);
        this.responseData.setSuccess(false);
        if (this.responseData.isError()) {
            responseData.setCode("500");
            this.responseData.setMessage("服务器内部错误");
        }
        else {
            responseData.setCode("400");
            this.responseData.setMessage("请求成功，格式错误");
        }
        return this.responseData;
    }*/
}
